<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      title="编辑程序名"
      :width="modalWidth"
      @cancel="handleCancel"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleCancel">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <div>
        <a-form-model
          :model="model"
          :rules="rules"
          ref="ruleForm"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item label="程序名:" prop="newProduceName">
            <a-input v-model="model.newProduceName" />
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-modal>
  </div>
</template>
<script>
import api from "@/api/apiServer";

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      visible: false,
      confirmLoading: false,
      model: {
        newProduceName: "",
      },
      modalWidth: 500, // 抽屉宽度
      idList: [],
      rules: {
        newProduceName: [
          { required: true, message: "内容不能为空", trigger: "change" },
        ],
      },
    };
  },

  methods: {
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record, idList) {
      this.visible = true;
      this.model = Object.assign({}, record);
      if (idList) {
        this.$set(this.model, "idList", idList);
      } else {
        this.$set(this.model, "idList", [record.opId]);
      }
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    async submitVulFun() {
      this.confirmLoading = true;
      const res = await api.PostAmenDrouTineName(this.model);
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        this.model = Object.assign({}, this.model);
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
    },
    handleCancel() {
      this.visible = false;
      this.model = Object.assign({});
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>
<style scoped></style>
